package com.byb.server.service.impl;

import com.byb.server.service.WcIsurerService;
import com.byb.server.domain.WcIsurer;
import com.byb.server.repository.WcIsurerRepository;
import com.byb.server.repository.search.WcIsurerSearchRepository;
import com.byb.server.service.dto.Wc.InsuranceForWcDTO;
import com.byb.server.service.dto.Wc.vm.InsuranceVM;
import com.byb.server.service.dto.Wc.vm.LetterVm;
import com.byb.server.service.dto.WcIsurerDTO;
import com.byb.server.service.mapper.Wc.InsuranceForWcMapper;
import com.byb.server.service.mapper.WcIsurerMapper;
import net.sourceforge.pinyin4j.PinyinHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;



import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;



import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing WcIsurer.
 */
@Service
@Transactional
public class WcIsurerServiceImpl implements WcIsurerService{

    private final Logger log = LoggerFactory.getLogger(WcIsurerServiceImpl.class);

    private final String[] letter= new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I",
        "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"  };

    private final WcIsurerRepository wcIsurerRepository;

    private final WcIsurerMapper wcIsurerMapper;

    private final WcIsurerSearchRepository wcIsurerSearchRepository;

    private final InsuranceForWcMapper insuranceForWcMapper;
    public WcIsurerServiceImpl(WcIsurerRepository wcIsurerRepository, WcIsurerMapper wcIsurerMapper, WcIsurerSearchRepository wcIsurerSearchRepository, InsuranceForWcMapper insuranceForWcMapper) {
        this.wcIsurerRepository = wcIsurerRepository;
        this.wcIsurerMapper = wcIsurerMapper;
        this.wcIsurerSearchRepository = wcIsurerSearchRepository;
        this.insuranceForWcMapper = insuranceForWcMapper;
    }

    /**
     * Save a wcIsurer.
     *
     * @param wcIsurerDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public WcIsurerDTO save(WcIsurerDTO wcIsurerDTO) {
        log.debug("Request to save WcIsurer : {}", wcIsurerDTO);
        WcIsurer wcIsurer = wcIsurerMapper.toEntity(wcIsurerDTO);
        wcIsurer = wcIsurerRepository.save(wcIsurer);
        WcIsurerDTO result = wcIsurerMapper.toDto(wcIsurer);
        //wcIsurerSearchRepository.save(wcIsurer);
        return result;
    }

    /**
     *  Get all the wcIsurers.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<WcIsurerDTO> findAll(Pageable pageable) {
        log.debug("Request to get all WcIsurers");
        return wcIsurerRepository.findAll(pageable)
            .map(wcIsurerMapper::toDto);
    }

    /**
     *  Get one wcIsurer by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public WcIsurerDTO findOne(Long id) {
        log.debug("Request to get WcIsurer : {}", id);
        WcIsurer wcIsurer = wcIsurerRepository.findOne(id);
        return wcIsurerMapper.toDto(wcIsurer);
    }

    /**
     *  Delete the  wcIsurer by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete WcIsurer : {}", id);
        wcIsurerRepository.delete(id);
        //wcIsurerSearchRepository.delete(id);
    }

    /**
     * Search for the wcIsurer corresponding to the query.
     *
     *  @param query the query of the search
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<WcIsurerDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of WcIsurers for query {}", query);
        Page<WcIsurer> result = wcIsurerSearchRepository.search(queryStringQuery(query), pageable);
        return result.map(wcIsurerMapper::toDto);
    }

    /**
     *  Get all the wcIsurers for Wc.
     *
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public InsuranceForWcDTO findAllForWc() {
        log.debug("Request to get all WcIsurers");
        List<InsuranceVM> list = wcIsurerRepository.findAll().stream().map(insuranceForWcMapper::toDto).collect(Collectors.toList());
        List<LetterVm> result = new ArrayList<>();
        List<InsuranceVM> data=new ArrayList<>();
        for (int i = 0; i < letter.length; i++) {
            LetterVm letterVm = new LetterVm();
            letterVm.setKey(letter[i]);
            result.add(letterVm);
        }
        for (int i = 0; i < list.size() ; i++) {
            for (int j = 0; j < letter.length ; j++) {
                if( result.get(j).getKey().equals(getshou(list.get(i).getName()))){
                    result.get(j).addItemList_item(list.get(i));
                }
            }
        }
        if(list.size()>=5){
            data=list.subList(0,4);
        }else{
            data=list;
        }

        return new InsuranceForWcDTO(data,result);



    }

    /**
     *
     * @param str
     * @return
     */
    public static String getshou(String str){
        String result =null;
        String s = getPinYinHeadChar(str);
        StringBuffer sb = new StringBuffer(s);
        if (sb.length() > 1) {
            result  = sb.delete(1, sb.length()).toString();
        }
        return String.valueOf(Character.toUpperCase(result.toCharArray()[0]));
    }

    /**
     * 获取字符首字母
     * @param str
     * @return
     */
    public static String getPinYinHeadChar(String str)
    {
        String convert = "";
       for (int j = 0; j < str.length(); j++)
        {
            char word = str.charAt(j);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null)
            {
                convert += pinyinArray[0].charAt(0);
            } else
            {
                convert += word;
            }
        }
        return convert;
    }
}
